<?php
namespace app\fsadmin\controller;

use think\Controller;

class LoginController extends Controller
{

    public function index()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();

            //验证验证码
            if (!captcha_check($params['code'])) {
                $msg['msg'] = "验证码错误";
            };

            //查找管理员
            $admin_m           = model("admin");
            $where['account'] = $params['name'];
            $admin             = $admin_m->getAdmin($where);

            //管理员存在则判断密码是否正确
            if ($admin) {
                $pwd = sha1md5($params['pwd']);
                if ($pwd == $admin['pwd']) {
                    session('admin', $admin);
                    //更新登录信息
                    $data['login_ip']   = ip2long($this->request->ip());
                    $data['login_time'] = time();
                    $admin_m->saveAdmin($data, ["id" => $admin['id']]);
                    $msg['status']      = 200;
                    $msg['msg']         = "登录成功";
                    $msg['url'] = url("index/index");
                } else {
                    $msg['msg'] = "密码错误";
                }
            } else {
                $msg['msg'] = "此用户不存在";
            }

            ajax_return($msg);
        }

        //取得网站配置信息
        $conf = cache('conf');
        if (!$conf) {
            $config_m = model("config");
            $conf     = $config_m->getConfByKey("conf");

            $conf = unserialize($conf->value);
            cache('conf', $conf);
        }
        $this->assign("conf", $conf);
        return $this->fetch();
    }


    public function loginout()
    {
        session(null);
        $this->redirect("index");
    }

}
